﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using common;
using ForkCarRelationManager.Utility;
using ForkCarRelationManager.DB;
namespace ForkCarRelationManager.Controls
{
    public partial class ScompanyControl : UserControl
    {
        Util util = new Util();
        ScompanyBI companyBi = new ScompanyBI();
        DataPaging ScompanyPage;
        DataSet ScompanyDataSet;
        dbOperate dbo = new dbOperate();
        bool ifedit=false;
        public ScompanyControl()
        {
            InitializeComponent();
        }

     

     

        private void ScompanyControl_Load(object sender, EventArgs e)
        {
            ScompanyPage = companyBi.GetScompanypage();
            Initdata();
        }
        private void Initdata()
        {
            ScompanyDataSet = ScompanyPage.Homepage();
            Scompanydatagrid.DataSource = ScompanyDataSet;
            Scompanydatagrid.DataMember = "data";
        }
    

        private void Scompanydatagrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Scompanydatagrid.CurrentRow != null)
            {
                groupPanel1.Text = "编辑模式";
                ifedit = true;
                string id = Scompanydatagrid.CurrentRow.Cells[(Scompanydatagrid.CurrentRow.Cells.Count) - 1].Value.ToString();

                DataTable scompanyInfor = companyBi.Getscompanyinfor(Convert.ToInt32(id));
            
                Scompanynametext.Text = scompanyInfor.Rows[0][0].ToString() ;
                ScompanyLocatext.Text = scompanyInfor.Rows[0][1].ToString();
                managerNametext.Text = scompanyInfor.Rows[0][2].ToString();
                managerPhonetext.Text = scompanyInfor.Rows[0][3].ToString();
                Scompanysumtext.Text = scompanyInfor.Rows[0][4].ToString();
            }

        }

        private void Scompanydatagrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Scompanydatagrid.CurrentRow != null)
            {
                groupPanel1.Text = "编辑模式";
                ifedit = true;
                string id = Scompanydatagrid.CurrentRow.Cells[(Scompanydatagrid.CurrentRow.Cells.Count) - 1].Value.ToString();

                DataTable scompanyInfor = companyBi.Getscompanyinfor(Convert.ToInt32(id));

                Scompanynametext.Text = scompanyInfor.Rows[0][0].ToString();
                ScompanyLocatext.Text = scompanyInfor.Rows[0][1].ToString();
                managerNametext.Text = scompanyInfor.Rows[0][2].ToString();
                managerPhonetext.Text = scompanyInfor.Rows[0][3].ToString();
                Scompanysumtext.Text = scompanyInfor.Rows[0][4].ToString();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
           
            if (Scompanydatagrid.CurrentRow != null&&ifedit)
            {
                
                string id = Scompanydatagrid.CurrentRow.Cells[(Scompanydatagrid.CurrentRow.Cells.Count) - 1].Value.ToString();
                string Scompanyname = Scompanynametext.Text;
                string ScompanyLoca = ScompanyLocatext.Text;
                string managerPhone = managerPhonetext.Text;
                string managerName = managerNametext.Text;
                string Scompanysum = Scompanysumtext.Text;
                if (Scompanyname.Trim() != "")
                {
                    if (companyBi.Updatescompany(Scompanyname, ScompanyLoca, managerPhone, managerName, Scompanysum, Convert.ToInt32(id)))
                    {
                        Initdata();
                        MessageBox.Show("修改成功!");
                    }
                    else
                        MessageBox.Show("修改失败!");
                }
                else
                    MessageBox.Show("公司名字不能为空!");
            }
            else if ( !ifedit)
            {
                string Scompanyname = Scompanynametext.Text;
                string ScompanyLoca = ScompanyLocatext.Text;
                string managerPhone = managerPhonetext.Text;
                string managerName = managerNametext.Text;
                string Scompanysum = Scompanysumtext.Text;
                if (Scompanyname.Trim() != "")
                {
                    if (companyBi.Addscompany(Scompanyname, ScompanyLoca, managerPhone, managerName, Scompanysum))
                    {
                        Initdata();
                        MessageBox.Show("添加成功");
                    }
                    else
                        MessageBox.Show("添加失败!");
                }
                else
                {
                    MessageBox.Show("公司名字不能为空!");
                }
            }
        }

        private void Btn_addmrecord_Click(object sender, EventArgs e)
        {
            ifedit = false;
            groupPanel1.Text = "添加模式";
            Scompanynametext.Text = "";
            ScompanyLocatext.Text = "";
            managerNametext.Text = "";
            managerPhonetext.Text = "";
            Scompanysumtext.Text = "";

        }

        //导出excel
        private void button5_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(this.Scompanydatagrid, ScompanyPage.allData(), 1);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
           
        }
    }
}
